﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

namespace Bus_proj.website
{
    public partial class add_ticket : System.Web.UI.Page
    {
        string date, time, date2, input_s;
        int sell_id, check, alertt;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    if (Int32.Parse(Session["login_Access_id"].ToString()) == 2)
                    {

                    }
                    else
                    {
                        Response.Redirect("login.aspx");
                    }
                }
                catch (Exception)
                {
                    Response.Redirect("login.aspx");
                }

            }     
        }

        public string ConvestToDate_check(string s)
        {
            int day = 1;
            int mount = 1;
            int years = 1111;
            try
            {
                string[] stemp = s.Split(' ');
                stemp = stemp[0].Split('/');
                day = Convert.ToInt32(stemp[0]);
                mount = Convert.ToInt32(stemp[1]);
                years = Convert.ToInt32(stemp[2]);
                if (years < 2300)
                {
                    years = years + 543;
                }
                string years2 = years.ToString();
                string mount2 = mount.ToString();
                string day2 = day.ToString();
                return (years2 + "/" + mount2 + "/" + day2);
            }
            catch (Exception)
            {

            }
            string years3 = years.ToString();
            string mount3 = mount.ToString();
            string day3 = day.ToString();
            return (years3 + "/" + mount3 + "/" + day3);
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string Connection = ConfigurationManager.ConnectionStrings["Bus_proConnectionString"].ConnectionString;
            string chk_pay = TextBox1.Text;
            string ticket2 = @"select *
                                from Table_payment
                                inner join  Table_Sale
                                on Table_payment.Sale_id = Table_Sale.Sale_id
                                INNER join Table_Around
                                    on Table_Sale.Around_id = Table_Around.Around_id
                                INNER join Table_Route
                                    on Table_Around.Route_id = Table_Route.Route_id
                                INNER join Table_Time
                                    on Table_Around.Time_id = Table_Time.Time_id
                                inner join Table_SaleDetail
	                                on Table_SaleDetail.Sale_id = Table_payment.Sale_id
                                    where Table_payment.payment_id = '" + chk_pay + "' and (SSID =1 or SSID =5) and Table_payment.payment_status = 1 ";
            SqlCommand sqlcomm2 = new SqlCommand();
            SqlConnection sqlconn = new SqlConnection(Connection);
            SqlDataAdapter da_ticket2 = new SqlDataAdapter(ticket2, sqlconn);
            DataTable dt_ticket2 = new DataTable();
            da_ticket2.Fill(dt_ticket2);
            if (dt_ticket2.Rows.Count <= 0)
            {
                ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('ไม่สามารถออกตั๋วได้');", true);
            }
            else
            {
                date = ConvestToDate_check(dt_ticket2.Rows[0]["Around_date"].ToString());
                time = dt_ticket2.Rows[0]["Time_time"].ToString();
                date2 = date + " " + time + ":00";
                string testt = (DateTime.Now.ToString());
                DateTime test = DateTime.Parse(testt);
                string data_ticket = "";
                string[] ticket_print = new string[100];
                if (DateTime.Parse(date2) >= DateTime.Now)
                {
                    //int data_count = 0;
                    for (int i = 0; i < dt_ticket2.Rows.Count; i++)
                    {                        
                       ticket_print[i] = dt_ticket2.Rows[i]["Sale_id"].ToString();                            
                       
                    }
                    //string[] change = data_ticket.Split(',');
                    Session["ticket_print"] = ticket_print;
                    Response.Redirect("report_ticket2.aspx");
                }
                else
                {
                    ScriptManager.RegisterClientScriptBlock(this.Page, this.Page.GetType(), "alert", "alert('ไม่สามารถออกตั๋วได้');", true);
                }
            }
        }
    }
}